package wiki.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.contents.Content;
import wiki.generic.io.Element;
import wiki.generic.utilities.AlreadyExistingElementException;
import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile della scelta delle immagini e inserimento media
 */
public class ImageChooseServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private boolean checked=false;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc=(Boolean) ses.getAttribute("rights");
			if(isDoc){
			String name=new String();
		boolean modify=req.getParameterMap().containsKey("name");
		if(modify){
			name=req.getParameter("name");
		}
		PrintWriter out = resp.getWriter();
		out.write("<html>\n");
		out.write("<head>\n");
		out.write("<title>Scelta Immagine</title>\n");
		out.write("<link rel='stylesheet' type='text/css' href='./html/edit/aggiungiimg.css'/>\n");
		out.write("<script type='text/JavaScript' src='./html/edit/aggiungiimg.js'></script>\n");
		out.write("</head>\n");
		out.write("<body>\n");
		out.write("<div id='title'>\n");
		if(!modify){
			out.write("Inserisci Immagine\n");
		} else{
			out.write("Modifica Immagine\n");
		}
		out.write("</div>\n");
		out.write("<div id='container'>\n");
		if(!modify){
			out.write("<form action='./img?type=img' method='post' name='modulo' id='modulo'>\n");
		} else {
			out.write("<form action='./img?type=img&nome="+name+"&mod=mod' method='post' name='modulo' id='modulo'>\n");
		}
		out.write("<table>\n");
		out.write("<div id='show_message'> I seguenti campi sono obbligatori*:</div>\n");
		out.write("<tr>\n");
		if(!modify) {
			out.write("<td class='col1'>Nome<span id='star_nome'>*</span>:&nbsp;&nbsp;&nbsp;<input class='testo' type='text' name='nome' value=''/></td>\n");
		}
		out.write("</tr>\n");
		out.write("</table>\n");
		if(!modify){
			out.write("<div id='titleimg'>Il nome sar&agrave; visualizzato se il browser utilizzato non supporta le immagini,<br />e posizionando il cursore sull'immagine stessa.</div>\n");
		}
		out.write("<br />\n");
		out.write("<br />\n");
		out.write("<h3 id='h3Color'>Scegli l'immagine:</h3>\n");
		out.write("<table>");
		
		HashMap<Integer, Element> imgMap=Setup.getSetup().getMediator().getVservice().getElementsMapbyType("img");
		Set<Integer> keys=imgMap.keySet();
		if(imgMap.size()==0){
			resp.sendRedirect("./html/errorpages/nofilecaricati.html");
		}
		int i=0;
		out.write("<tr>\n");
		out.write("<ul>\n");
		Content c=null;
		if(modify){
			String corso=(String) ses.getAttribute("corso");
			String current=(String) ses.getAttribute("current");
			try {
				c=Setup.getSetup().getMediator().getpService().getContentbyName(corso, current, name);
			} catch (PageNotFoundException e) {
				resp.sendRedirect("./html/errorpages/pagina404.html");
			}
		}
		
		
		for (Iterator<Integer> iterator = keys.iterator(); iterator.hasNext();) {
			Integer key =iterator.next();
			if(i%4==0 && i!=0){
				out.write("</ul>\n");
				out.write("</tr>\n");
				out.write("<tr>\n");
				out.write("<ul>\n");
			}
			out.write("<td><li><input type='radio' value='"+key+"' name='cont'"); 
			if(i==0 || (modify && c.getID()==key)){
				out.write(" checked='checked' ");
			}
			out.write("/><br /><img src='"+imgMap.get(key).getInfo()+"' class='imgstile'/></li></td>\n");
			i++;
		}
		out.write("</table>\n");
		out.write("<br />\n");
		if(modify){
			out.write("<input type='submit' value='Aggiungi' id='bottone_sup' name='submit' />\n");
		}else{
			out.write("<input type='submit' value='Aggiungi' id='bottone_sup' name='submit' onclick=\"if(controllaVuoti('modulo','star_nome','show_message')!=true){return false;}\" />\n");
		}
		out.write("<br/><br/><br/>\n");
		out.write("<div id='style-contents'><h3 id='h3Color'>Oppure caricane una nuova:</h3>\n");
		out.write("<br/>\n");
		if(!modify){
			out.write("<a href='./upload'><input type='button' value='Upload' id='bottone_upload' name='but'/></a>\n");
		} else {
			out.write("<a href='./upload?mod=mod&type=img&name="+c.getContentName()+"&ID="+c.getID()+"' ><input type='button' value='Upload' id='bottone_upload' name='but'/></a>\n");
		}
		out.write("</div>");
		out.write("<h3> Dimensione e Allineamento:</h3>\n");
		out.write("<ul id='fontsyle-list'>\n");
		
		checked=false;
		
		String[][] sizes=new String[3][2];
		sizes[0][0]="Centrale";
		sizes[1][0]="Destra";
		sizes[2][0]="Sinistra";
		out.write("Piccola:\n");
		
		sizes[0][1]="width:100px; height:auto; margin-left:272px;";
		sizes[1][1]="width:100px; height:auto; margin-left:530px;";
		sizes[2][1]="width:100px; height:auto;";
		
		String comp="";
		if(modify){
			comp=c.getAttributes().get(0);
		}
		out.write(getPosizions(sizes, 3, modify,comp , false));
		out.write("<br/>\n");
		out.write("Media:\n");
		
		sizes[0][1]="width:200px; height:auto; margin-left:220px;";
		sizes[1][1]="width:200px; height:auto; margin-left:430px;";
		sizes[2][1]="width:200px; height:auto;";
		
		out.write(getPosizions(sizes, 3, modify, comp, true));
		out.write("<br/>\n");
		out.write("Grande:\n");
		
		sizes[0][1]="width:300px; height:auto; margin-left:170px;";
		sizes[1][1]="width:300px; height:auto; margin-left:330px;";
		sizes[2][1]="width:300px; height:auto;";
		
		out.write(getPosizions(sizes, 3, modify, comp, false));
		
		out.write("<br/>\n");
		out.write("Molto Grande:\n");
		
		sizes[0][1]="width:400px; height:auto; margin-left:120px;";
		sizes[1][1]="width:400px; height:auto; margin-left:230px;";
		sizes[2][1]="width:400px; height:auto;";
		
		out.write(getPosizions(sizes, 3, modify, comp, false));
		
		out.write("<br/>");
		if(modify){
			out.write("<input type='submit' value='Aggiungi' id='bottone' name='submit' />\n");
		}else{
			out.write("<input type='submit' value='Aggiungi' id='bottone' name='submit' onclick=\"if(controllaVuoti('modulo','star_nome','show_message')!=true){return false;}\" />\n");
		}
		out.write("</form>\n");
		out.write("</div>\n");
		out.write("</div>\n");
		out.write("</body>\n");
		out.write("</html>\n");
			}else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else{
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
	}

	private String getPosizions(String[][] sizes, int choises, boolean modify, String comparison, boolean def){
		String s="";
		for (int i = 0; i < choises; i++) {
			s+="<li><input type='radio' class='stile' value='"+sizes[i][1]+"' name='size' ";
			if((i==0 && !checked && def) || (modify && comparison.equals(sizes[i][1]))){
				s+="checked='checked' ";
				checked=true;
			}
			s+="/>"+sizes[i][0]+"</li>\n";
		}
		return s;
	}
	
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc=(Boolean) ses.getAttribute("rights");
			if(isDoc){
		String corso=(String) ses.getAttribute("corso");
		String type=req.getParameter("type");
		if(type!=null){
		String nome=req.getParameter("nome");
		if(nome==null || nome.equals("")){
			nome="___";
		}
		nome=nome.replace(" ", "_");
		nome=Setup.getSetup().getMediator().getGpService().changeInputText(nome);
		int ID=Integer.parseInt(req.getParameter("cont"));
		String size=req.getParameter("size");
		List<String> attr=new ArrayList<String>();
		attr.add(size);
		String current = (String)ses.getAttribute("current");
		try {
			if(!req.getParameterMap().containsKey("mod")){
				Setup.getSetup().getMediator().geteService().appendContent(corso,current,nome,type, ID, attr);
			} else {
				Setup.getSetup().getMediator().geteService().modifyContent(corso,current, nome, ID, attr);
			}
			resp.sendRedirect("./view?page="+current);
		} catch (AlreadyExistingElementException e) {
			resp.sendRedirect("./html/errorpages/nomecontenuto.html"); 
		} catch (PageNotFoundException e) {
			resp.sendRedirect("./html/errorpages/pagina404.html");
		}
		} else {
			resp.sendRedirect("./html/errorpages/nopermessi.html");
			return;
		}
	} else{
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
	}

}

	
}



}
